﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using ElectrosLtd.Models;
using Common.Views;
using System.IO;
using iTextSharp.text;
using System.Net.Mail;
using System.Net;

namespace ElectrosLtd.Controllers
{
    public class PartialController : Controller
    {
        public PartialViewResult _LogOn()
        {
            LogOnPartialModel lopn = new LogOnPartialModel();
            lopn.NoOfItemsInCart = new UsersServ.WCFUsersClient().GetNoOfItemsInShoppingCart(HttpContext.User.Identity.Name);
            return PartialView("_LogOn", lopn);
        }

        public PartialViewResult Menus()
        {
            List<Menu> menus = new List<Menu>();

            if (HttpContext.User.Identity.Name == "")
            {
                menus = new UsersServ.WCFUsersClient().GetMainMenus(3).ToList();
            }
            else
            {
                List<Role> thisR = new UsersServ.WCFUsersClient().GetUserRoles(HttpContext.User.Identity.Name).ToList();

                foreach (Role r in thisR)
                {
                    int role = r.UserTypeID;

                    if (HttpContext.User.Identity.Name == "")
                    {
                        menus = new UsersServ.WCFUsersClient().GetMainMenus(3).ToList();
                        break;
                    }
                    else
                    {
                        if (role == 1)
                        {
                            menus = new UsersServ.WCFUsersClient().GetMainMenus(role).ToList();
                            break;
                        }
                        if (role == 2)
                        {
                            menus = new UsersServ.WCFUsersClient().GetMainMenus(role).ToList();
                            break;
                        }
                    }
                }
            }

            return PartialView("_Menus", menus);
        }

        public PartialViewResult _Comments(int id)
        {
            List<Comment> comments = new ProductsServ.WCFProductsClient().GetCommentsForProduct(id).ToList();
            return PartialView("_Comments", comments);
        }

        public PartialViewResult _SearchProductByName()
        {
            return PartialView(new SearchProductByNameModel());
        }
        
        public PartialViewResult _FilterProducts()
        {
            List<Category> categoryList = new ProductsServ.WCFProductsClient().GetCategories().ToList();
            return PartialView("_FilterProducts", categoryList);
        }

        

        [Authorize]
        public PartialViewResult _FilterFaults()
        {
            new ProductsServ.WCFProductsClient().GetProductNameInUserFaults(HttpContext.User.Identity.Name);
            return PartialView(new FilterFaultsModel()); 
        }

        public PartialViewResult GetHighestRatedProduct()
        {
            IEnumerable<StatisticalReportView> sr = new UsersServ.WCFUsersClient().GetTopRatedProducts();
            return PartialView("GetHighestRatedProduct",sr);
        }

        public PartialViewResult GetMostPurchasedProducts()
        {
            IEnumerable<StatisticalReportView> sr = new UsersServ.WCFUsersClient().GetMostPurchasedProduct();
             return PartialView("GetMostPurchasedProducts", sr);
        }

        public PartialViewResult GetMostFaultsForProduct()
        {
            IEnumerable<StatisticalReportView> sr = new UsersServ.WCFUsersClient().GetProductWithHighestNumberOfFaults();
             return PartialView("GetMostFaultsForProduct", sr);
        }

        public PartialViewResult GetLeastFaultsForProduct()
        {
            IEnumerable<StatisticalReportView> sr = new UsersServ.WCFUsersClient().GetProductWithLeastNumberOfFaults();
            return PartialView("GetLeastFaultsForProduct", sr);
        }        
        
        [HttpGet]
        public ActionResult DateListed()
        {
            List<Product> ProductList = new ProductsServ.WCFProductsClient().GetProductsByDateListed().ToList();
            return View("../Home/Index", ProductList);
        }

        [HttpGet]
        public ActionResult FilterFaultsByDate()
        {
            List<FaultDetailsView> FDV = new UsersServ.WCFUsersClient().FilterFaultsByDate().ToList();
            return View("../Home/UpdateFaults", FDV);
        }

        

        [HttpGet]
        public ActionResult PriceAscending()
        {
            List<Product> ProductList = new ProductsServ.WCFProductsClient().GetProductsByPriceAsc().ToList();
            return View("../Home/Index", ProductList);
        }

        [HttpGet]
        public ActionResult PriceDescending()
        {
            List<Product> ProductList = new ProductsServ.WCFProductsClient().GetProductsByPriceDesc().ToList();
            return View("../Home/Index", ProductList);
        }

        [HttpGet]
        public ActionResult Category(int id)
        {
            List<Product> ProductList = new ProductsServ.WCFProductsClient().GetProductByCategory(id).ToList();
            return View("../Home/Index", ProductList);
        }

       

    }
}
